from django import forms
from festivalrock.models import Descuento, Sector, DescuentoAnticipada, Festival,\
    PuntoVenta, Noche, Presentacion, Banda
from festivalrock.utils import CurrencyField


class EmitirTicketForm(forms.Form):
    DESCUENTOS_CHOICE = [( descuento.id, descuento.__str__() ) for descuento in Descuento.objects.all().exclude(id__in=DescuentoAnticipada.objects.all()) ]
    descuento = forms.ModelChoiceField(empty_label='Aplicar Descuento', queryset=DESCUENTOS_CHOICE, required=False)

class FiltrarLocalidadesForm(forms.Form):
    SECTOR_CHOICE = [( sector.id, sector.__str__() ) for sector in Sector.objects.all()]
    sector = forms.ChoiceField(label='filtrar por', choices=[(1,"Seleccione un Sector")] + SECTOR_CHOICE, required=False, widget=forms.Select(attrs={'onchange': 'this.form.submit();'})) 

class FiltrarFestivalesForm(forms.Form):
    FESTIVALES_CHOICE = [ (festival.nombre) for festival in Festival.objects.all() ]
    festival = forms.ModelChoiceField(empty_label='Nombre', queryset=FESTIVALES_CHOICE, required=False, widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))


class Venta_FestivalesForm(forms.ModelForm):
    festivales=forms.ChoiceField(label='Festival')

    def __init__(self, id_puntoVenta, request, *args, **kwargs):
        super(Venta_FestivalesForm, self).__init__(*args, **kwargs)
        FESTIVALES_CHOICE = [(0,' ')]+[ (festival.id, festival.__str__()) for festival in Festival.objects.filter(puntoventa=id_puntoVenta) ]
        self.fields['festivales'].widget = forms.Select(attrs={'onchange': 'this.form.submit();'}, choices=FESTIVALES_CHOICE)
        
    def setFestivalSeleccionado(self, id_festival):
        self.fields['festivales'].initial = id_festival
        
    class Meta:
        model = PuntoVenta
        fields=('festivales',)

class Venta_DiasForm(forms.ModelForm):
    noches=forms.ChoiceField(label='Dia')
    
    def __init__(self, festival_seleccionado, request, *args, **kwargs):
        super(Venta_DiasForm, self).__init__(*args, **kwargs)
        NOCHES_CHOICE= [(0,' ')]+[ (noche.id, noche.__str__()) for noche in Noche.objects.filter(festival=festival_seleccionado) ]
        self.fields['noches'].widget = forms.Select(attrs={'onchange': 'this.form.submit();'}, choices=NOCHES_CHOICE)
    
    def setNocheSeleccionada(self, id_noche):
        self.fields['noches'].initial = id_noche
            
    class Meta:
        model = Festival
        fields=('noches',)

class Venta_SectoresForm(forms.ModelForm):
    sectores=forms.ChoiceField(label='Sector')
    
    def __init__(self, noche_seleccionada, request, *args, **kwargs):
        super(Venta_SectoresForm, self).__init__(*args, **kwargs)
        SECTORES_CHOICE = [(0,' ')]+[ (sector.id, sector.__str__()) for sector in Sector.objects.filter(noche=noche_seleccionada) ]
        self.fields['sectores'].widget = forms.Select(attrs={'onchange': 'this.form.submit();'}, choices=SECTORES_CHOICE)

    def setSectorSeleccionado(self, id_sector):
        self.fields['sectores'].initial = id_sector
        
    class Meta:
        model = Festival
        fields=('sectores',)

DESCUENTOS_CHOICE = [(0,' ')]+[(descuento.id, descuento.__str__()) for descuento in Descuento.objects.all().exclude(id__in=DescuentoAnticipada.objects.all())]
        
class Venta_EntradasForm(forms.Form):
    fila = forms.IntegerField(max_value=3)
    butaca = forms.IntegerField(max_value=3)
    importeEntradaBase = CurrencyField(max_digits=10, decimal_places=2, blank=True)
    adicionalCategoria = CurrencyField(max_digits=10, decimal_places=2, blank=True)
    descuento = forms.Select(choices=DESCUENTOS_CHOICE)
    importeTotal = CurrencyField(max_digits=10, decimal_places=2, blank=True)

class Planificacion_FestivalesForm(forms.ModelForm):
    festivales=forms.ChoiceField(label='Festival')

    def __init__(self, id_puntoVenta, request, *args, **kwargs):
        super(Planificacion_FestivalesForm, self).__init__(*args, **kwargs)
        FESTIVALES_CHOICE = [(0,' ')]+[ (festival.id, festival.__str__()) for festival in Festival.objects.filter(puntoventa=id_puntoVenta) ]
        self.fields['festivales'].widget = forms.Select(choices=FESTIVALES_CHOICE)
        
    def setFestivalSeleccionado(self, id_festival):
        self.fields['festivales'].initial = id_festival
        
    class Meta:
        model = PuntoVenta
        fields=('festivales',)

BANDAS_CHOICE = [(0,' ')]+[ (banda.id, banda.__str__()) for banda in Banda.objects.all() ]
    
class Planificacion_BandasForm(forms.ModelForm):
    bandas=forms.ChoiceField(label='Banda')
    
    def __init__(self, request, *args, **kwargs):
        super(Planificacion_BandasForm, self).__init__(*args, **kwargs)
        self.fields['bandas'].widget = forms.Select(choices=BANDAS_CHOICE)
    
    class Meta:
        model = Presentacion
        fields=('bandas',)
